unit Class_GKJL_FL_IN_GKZF;

interface
uses
  Classes,SysUtils,Uni,UniEngine,Class_GKJL_EL_IN_GKZF;

type
  TGKJLFL=class(TUniEngine)
  private
    FGKJGIDEX: Integer;
    FUNITLINK: string;
    FGKJLIDEX: Integer;
    FGKJLFLID: Integer;
    FGKJGFLID: Integer;
    FDATADOUB: Extended;
    FDATATEXT: string;
    FDATAMEMO: string;
  private
    //not in datebase
    FCORDIDEX: Integer;
    FCORDATTR: Integer;
  protected
    procedure SetParameters;override;
    function  GetStrInsert:string;override;
    function  GetStrUpdate:string;override;
    function  GetStrDelete:string;override;
  public
    function  GetStrsIndex:string;override;
  public
    function  GetNextIdex:Integer;overload;
    function  GetNextIdex(AUniConnection:TUniConnection):Integer;overload;

    procedure CheckBrotherInGKJG(AUniConnection:TUniConnection;AListJGFL:TStringList);
  public
    function  CheckExist(AUniConnection:TUniConnection):Boolean;override;
  public
    destructor  Destroy; override;
    constructor Create;  
  published
    property GKJGIDEX: Integer read FGKJGIDEX write FGKJGIDEX;
    property UNITLINK: string read FUNITLINK write FUNITLINK;
    property GKJLIDEX: Integer read FGKJLIDEX write FGKJLIDEX;
    property GKJLFLID: Integer read FGKJLFLID write FGKJLFLID;
    property GKJGFLID: Integer read FGKJGFLID write FGKJGFLID;
    property DATADOUB: Extended read FDATADOUB write FDATADOUB;
    property DATATEXT: string read FDATATEXT write FDATATEXT;
    property DATAMEMO: string read FDATAMEMO write FDATAMEMO;

    property CordIdex: Integer read FCordIdex write FCordIdex;
    property CordAttr: Integer read FCordAttr write FCordAttr;
  public
    class function  ReadDS(AUniQuery:TUniQuery):TUniEngine;override;
    class procedure ReadDS(AUniQuery:TUniQuery;var Result:TUniEngine);override;
    class function  CopyIt(AGKJLFL:TGKJLFL):TGKJLFL;overload;        
    class procedure CopyIt(AGKJLFL:TGKJLFL;var Result:TGKJLFL);overload;

    class procedure CopyIn(AGKJLEL:TGKJLEL;var Result:TGKJLFL);

    class function  ExpSQL:string;
    class function  ExpSQL_IN_VIEW:string;
    class function  ExpSQL_NO_VIEW(ADJLX:Integer):string;
  end;

implementation

uses
  Class_GKJG_FL_IN_GKZF,GkxContxt;

{ TGKJLFL }
procedure TGKJLFL.SetParameters;
begin
  inherited;
  with FUniSQL.Params do
  begin
    case FOptTyp of
      otAddx:
      begin
        ParamByName('GKJG_IDEX').Value := GKJGIDEX;
        ParamByName('UNIT_LINK').Value := UNITLINK;
        ParamByName('GKJL_IDEX').Value := GKJLIDEX;
        ParamByName('GKJL_FLID').Value := GKJLFLID;
        ParamByName('GKJG_FLID').Value := GKJGFLID;
        ParamByName('DATA_DOUB').Value := DATADOUB;
        ParamByName('DATA_TEXT').Value := DATATEXT;
        ParamByName('DATA_MEMO').Value := DATAMEMO; 
      end;
      otEdit:
      begin
        ParamByName('GKJG_IDEX').Value := GKJGIDEX;
        ParamByName('UNIT_LINK').Value := UNITLINK;
        ParamByName('GKJL_IDEX').Value := GKJLIDEX;
        ParamByName('GKJL_FLID').Value := GKJLFLID;
        ParamByName('GKJG_FLID').Value := GKJGFLID;
        ParamByName('DATA_DOUB').Value := DATADOUB;
        ParamByName('DATA_TEXT').Value := DATATEXT;
        ParamByName('DATA_MEMO').Value := DATAMEMO; 
      end;  
      otDelt:
      begin
        ParamByName('GKJG_IDEX').Value := GKJGIDEX;
        ParamByName('UNIT_LINK').Value := UNITLINK;
        ParamByName('GKJL_IDEX').Value := GKJLIDEX;
        ParamByName('GKJL_FLID').Value := GKJLFLID;
        ParamByName('GKJG_FLID').Value := GKJGFLID;
 
      end;  
    end;
  end;
end;

function TGKJLFL.CheckExist(AUniConnection: TUniConnection): Boolean;
begin
  Result:=CheckExist('GKX_GKJL_FL',['GKJG_IDEX',GKJGIDEX,'UNIT_LINK',UNITLINK,'GKJL_IDEX',GKJLIDEX,'GKJL_FLID',GKJLFLID,'GKJG_FLID',GKJGFLID],AUniConnection);
end;

function TGKJLFL.GetNextIdex: Integer;
begin

end;

function TGKJLFL.GetNextIdex(AUniConnection: TUniConnection): Integer;
begin

end;

function TGKJLFL.GetStrDelete: string;
begin
  Result:='DELETE FROM GKX_GKJL_FL WHERE   GKJG_IDEX=:GKJG_IDEX AND UNIT_LINK=:UNIT_LINK AND GKJL_IDEX=:GKJL_IDEX AND GKJL_FLID=:GKJL_FLID AND GKJG_FLID=:GKJG_FLID';
end;

function TGKJLFL.GetStrInsert: string;
begin
  Result:='INSERT INTO GKX_GKJL_FL'
         +'    ( GKJG_IDEX, UNIT_LINK, GKJL_IDEX, GKJL_FLID, GKJG_FLID'
         +'    , DATA_DOUB, DATA_TEXT, DATA_MEMO)'
         +'    VALUES'
         +'    (:GKJG_IDEX,:UNIT_LINK,:GKJL_IDEX,:GKJL_FLID,:GKJG_FLID'
         +'    ,:DATA_DOUB,:DATA_TEXT,:DATA_MEMO)';
end;

function TGKJLFL.GetStrsIndex: string;
begin
  Result:=Format('%D-%S-%D-%D-%D',[GKJGIDEX,UNITLINK,GKJLIDEX,GKJLFLID,GKJGFLID]);
end;

function TGKJLFL.GetStrUpdate: string;
begin
  Result:='UPDATE  GKX_GKJL_FL SET'
         +'    DATA_DOUB=:DATA_DOUB,'
         +'    DATA_TEXT=:DATA_TEXT,'
         +'    DATA_MEMO=:DATA_MEMO'
         +'    WHERE GKJG_IDEX=:GKJG_IDEX'
         +'    AND   UNIT_LINK=:UNIT_LINK'
         +'    AND   GKJL_IDEX=:GKJL_IDEX'
         +'    AND   GKJL_FLID=:GKJL_FLID'
         +'    AND   GKJG_FLID=:GKJG_FLID';
end;

constructor TGKJLFL.Create;
begin

end;

destructor TGKJLFL.Destroy;
begin

  inherited;
end;

class function TGKJLFL.ReadDS(AUniQuery: TUniQuery): TUniEngine;
begin
  Result:=TGKJLFL.Create;
  with TGKJLFL(Result) do
  begin
    GKJGIDEX:=AUniQuery.FieldByName('GKJG_IDEX').AsInteger;
    UNITLINK:=AUniQuery.FieldByName('UNIT_LINK').AsString;
    GKJLIDEX:=AUniQuery.FieldByName('GKJL_IDEX').AsInteger;
    GKJLFLID:=AUniQuery.FieldByName('GKJL_FLID').AsInteger;
    GKJGFLID:=AUniQuery.FieldByName('GKJG_FLID').AsInteger;
    DATADOUB:=AUniQuery.FieldByName('DATA_DOUB').AsFloat;
    DATATEXT:=AUniQuery.FieldByName('DATA_TEXT').AsString;
    DATAMEMO:=AUniQuery.FieldByName('DATA_MEMO').AsString;
  end;  
end;

class procedure TGKJLFL.ReadDS(AUniQuery: TUniQuery; var Result: TUniEngine);
begin
  if Result=nil then Exit;
  with TGKJLFL(Result) do
  begin
    GKJGIDEX:=AUniQuery.FieldByName('GKJG_IDEX').AsInteger;
    UNITLINK:=AUniQuery.FieldByName('UNIT_LINK').AsString;
    GKJLIDEX:=AUniQuery.FieldByName('GKJL_IDEX').AsInteger;
    GKJLFLID:=AUniQuery.FieldByName('GKJL_FLID').AsInteger;
    GKJGFLID:=AUniQuery.FieldByName('GKJG_FLID').AsInteger;
    DATADOUB:=AUniQuery.FieldByName('DATA_DOUB').AsFloat;
    DATATEXT:=AUniQuery.FieldByName('DATA_TEXT').AsString;
    DATAMEMO:=AUniQuery.FieldByName('DATA_MEMO').AsString;
  end;  
end;

class function  TGKJLFL.CopyIt(AGKJLFL: TGKJLFL): TGKJLFL;
begin
  Result:=TGKJLFL.Create;
  TGKJLFL.CopyIt(AGKJLFL,Result)
end;

class procedure TGKJLFL.CopyIt(AGKJLFL:TGKJLFL;var Result:TGKJLFL);
begin
  if Result=nil then Exit;
  Result.GKJGIDEX:=AGKJLFL.GKJGIDEX;
  Result.UNITLINK:=AGKJLFL.UNITLINK;
  Result.GKJLIDEX:=AGKJLFL.GKJLIDEX;
  Result.GKJLFLID:=AGKJLFL.GKJLFLID;
  Result.GKJGFLID:=AGKJLFL.GKJGFLID;
  Result.DATADOUB:=AGKJLFL.DATADOUB;
  Result.DATATEXT:=AGKJLFL.DATATEXT;
  Result.DATAMEMO:=AGKJLFL.DATAMEMO;

  Result.CordIdex:=AGKJLFL.CordIdex;
  Result.CordAttr:=AGKJLFL.CordAttr;
end;


class procedure TGKJLFL.CopyIn(AGKJLEL: TGKJLEL; var Result: TGKJLFL);
begin
  Result.FGKJGIDEX:=AGKJLEL.GKJGIDEX;
  Result.FUNITLINK:=AGKJLEL.UNITLINK;
  Result.FGKJLIDEX:=AGKJLEL.GKJLIDEX;
  Result.FGKJLFLID:=AGKJLEL.GKJLFLID;
end;

class function TGKJLFL.ExpSQL: string;
begin
  Result:='SELECT GKJG_IDEX'
         +'    ,UNIT_LINK'
         +'    ,GKJL_IDEX'
         +'    ,GKJL_FLID'
         +'    ,GKJG_FLID'
         +'    ,DATA_DOUB'
         +'    ,DATA_TEXT'
         +'    ,DATA_MEMO'
         +'    FROM GKX_GKJL_FL'
         +'    ORDER BY GKJL_FLID,GKJG_FLID';
end;

procedure TGKJLFL.CheckBrotherInGKJG(AUniConnection: TUniConnection;
  AListJGFL: TStringList);
var
  I:Integer;
  JGFLA:TGKJGFL;
  GKFLA:TGKJLFL;
begin
  if FCORDIDEX=-1 then Exit;
  if FCORDATTR=-1 then Exit;
  if FCORDATTR=0  then Exit;   //because it is code property.
  if FCORDATTR=2  then Exit;   //because it is memo property.
  
  if (AListJGFL=nil) or (AListJGFL.Count=0) then Exit;

  for I:=0 to AListJGFL.Count -1 do
  begin
    JGFLA:=TGKJGFL(AListJGFL.Objects[I]);
    if JGFLA=nil then Continue;

    if (JGFLA.CORDIDEX=FCORDIDEX) and (JGFLA.CORDATTR in [0,2]) then
    begin
      GKFLA:=TGKJLFL.Create;

      CopyIt(Self,GKFLA);
      GKFLA.FGKJGFLID:=JGFLA.GKJGFLID;
      GKFLA.FCORDATTR:=JGFLA.CORDATTR;

      case GKFLA.FCORDATTR of
        0:GKFLA.FDATATEXT:=GkxContxtEx.GetCodeTypF(self.DATATEXT,self.CordIdex);
        2:GKFLA.FDATATEXT:=GkxContxtEx.GetMemoTypF(self.DATATEXT,self.CordIdex);
      end;

      if GKFLA.FDATATEXT<>CONST_FETCH_ERROR then
      begin
        GKFLA.SaveItDB(AUniConnection);
      end;
      
      FreeAndNil(GKFLA);
    end;
  end;
end;

class function TGKJLFL.ExpSQL_IN_VIEW: string;
begin
  Result:='SELECT A.GKJG_IDEX'
         +'    ,A.UNIT_LINK'
         +'    ,A.GKJL_IDEX'
         +'    ,A.GKJL_FLID'
         +'    ,A.GKJG_FLID'
         +'    ,A.DATA_DOUB'
         +'    ,A.DATA_TEXT'
         +'    ,A.DATA_MEMO'
         +'    FROM GKX_GKJL_FL A'
         +'    WHERE EXISTS (SELECT 1 FROM VIEW_YOTO_GKEL B WHERE A.UNIT_LINK=B.UNIT_LINK AND A.GKJL_IDEX=B.GKJL_IDEX AND A.GKJG_IDEX=B.GKJG_IDEX AND   A.GKJL_FLID=B.GKJL_FLID)'
         +'    ORDER BY A.GKJL_FLID,A.GKJG_FLID';
end;

class function TGKJLFL.ExpSQL_NO_VIEW(ADJLX: Integer): string;
begin
  Result:='SELECT A.GKJG_IDEX'
         +'    ,A.UNIT_LINK'
         +'    ,A.GKJL_IDEX'
         +'    ,A.GKJL_FLID'
         +'    ,A.GKJG_FLID'
         +'    ,A.DATA_DOUB'
         +'    ,A.DATA_TEXT'
         +'    ,A.DATA_MEMO'
         +'    FROM GKX_GKJL_FL A'
         +'    WHERE EXISTS (SELECT 1 FROM GKX_GKJL_EL B  WHERE 1=1'
         +'      AND B.GKJL_DJLX=%D'
         +'      AND A.UNIT_LINK=B.UNIT_LINK'
         +'      AND A.GKJL_IDEX=B.GKJL_IDEX'
         +'      AND A.GKJG_IDEX=B.GKJG_IDEX'
         +'      AND A.GKJL_FLID=B.GKJL_FLID)'
         +'    ORDER BY A.GKJL_FLID,A.GKJG_FLID';
  Result:=Format(Result,[ADJLX]);
end;

end.

